<?php
// 本类由系统自动生成，仅供测试用途
namespace Admin\Controller;
use Think\Controller;
class StockController extends Controller {
    public function index(){
        $getadd = I("get.");
        $url = 'https://api.map.baidu.com/geocoder/v2/?callback=renderReverse&location='.$getadd['lat'].','.$getadd['lng'].'&output=json&pois=1&ak='.$getadd['ak'];
        // var url = 'http://api.map.baidu.com/geocoder/v2/?callback=renderReverse&location=39.983424,116.322987&output=json&pois=1&ak=HyBW0c8VaZA753vePenIDdIimqbbm5GM';$getadd['lng']
        // print_r($getadd);die;
        $html = file_get_contents($url); 
        print_r($html);die;
    }

    public function  editcangku(){
        $dat = I('post.');
        $stock = M('cars_stock');
        $res = $stock->where('id ='.$dat['id'])->save(array('stock'=>$dat['stock']));
        if($res){
            $this->ajaxReturn(array('ret'=>"OK",'msg'=>"库存修改成功"));
        }else{
            $this->ajaxReturn(array('ret'=>"ERROR",'msg'=>"库存修改失败"));
        }
    }
    public function  car_guanli(){
        // $cars   = D('cars');
        $cars   = M('qcgl_manage', null);
        $count= $cars -> count();// 查询满足要求的总记录数
        $Page = new \Think\Page($count,15);// 实例化分页类 传入总记录数和每页显示的记录数(25)
        $carsad = $cars -> limit($Page->firstRow.','.$Page->listRows) -> field('id, qcmc, cszdj, cjsj, sf, yg, xsjg, jb') -> order('cjsj desc')->select();
        // $adminid = M('admin')->where('id='.session('lampzuo_id'))->find();
        // $this->assign('adminid',$adminid);
        $this->assign('carsad',$carsad);
        $this->assign('page',$Page->show());
        $this->display();
    }
    /**
     * 车辆列表
     * @return [type] [description]
     */
    public function cangku(){
        $getadd    = I('get.');
        $carsstock = M('cars_stock');
        $Cars   = M('qcgl_manage', null);
        $City   = M('City');
        $carscount = $carsstock->where('1=1 and carnum='.$getadd['carid'])->select();
        $count1 = count($carscount);
        for($i = 0; $i < $count1; $i++){
            $sum1.=$sum1?",'".$carscount[$i]['provinceid']."'":"'".$carscount[$i]['provinceid']."'";
            $sum2.=$sum2?",'".$carscount[$i]['cityid']."'":"'".$carscount[$i]['cityid']."'";
        }
        $w  ="1=1";
        $w .=" and id in (".$sum2.")";
        $count  = $City->where($w)->count();// 查询满足要求的总记录数
        $Page   = new \Think\Page($count,15);
        $result = $City->where($w)->limit($Page->firstRow.','.$Page->listRows)->order('add_time desc')->select();
        $result1 = $City->where("1=1 and id in (".$sum1.")")->select();
        foreach ($result as $key => $value) {
            foreach ($result1 as $k => $v) {
                if($v['id'] == $value['pid']){
                    $result[$key]['names'] = $v['name'];
                }           
            }
        }
        foreach ($result as $key => $value) {
            foreach ($carscount as $ke => $v) {
                if($value['id'] == $v['cityid']){
                    $result[$key]['color'][] = $v;
                } 
            }
        }
        $this->assign('result',$result);
        
        $this->assign('page',$Page->show());
        $this->display();
    }
    /**
     * 添加车辆库存
     */
    public function addStock(){
        $stoke = I('post.');
        $city  = M('city');
        $stock = M('cars_stock');
        // echo  $city->getLastSql();
        $stockarr = array(
            'id'=>$stoke['provinceid'],
            'type'=>1,
            'pid'=>0,
            'name'=>$stoke['provincename'],
            'add_time'=>time(),
            );
        $arrname = $city->where(array('id'=>$stoke['provinceid']))->find();
       
        if($arrname){
            $arrid = $arrname['id'];
        }else{
            $arrid = $city->add($stockarr);
        }

        if($arrid){
            $pidkarr = array(
            'id'=>$stoke['cityid'],
            'type'=>2,
            'pid'=>$arrid,
            'name'=>$stoke['cityname'],
            'add_time'=>time(),
            );
            $cityname = $city->where(array('id'=>$stoke['cityid']))->find();
            if($cityname){
                $cityid = $cityname['id'];
            }else{
                $cityid = $city->add($pidkarr);
            }
            
            $sockarr = array(
                'carnum'=> $stoke['carnum'],
                'color'=> $stoke['color'],
                'provinceid'=> $arrid,
                'cityid'=> $cityid,
                'stock'=> $stoke['sockid'],
                'add_time'=> time(),
                );
            $stocklist = $stock->where(array('carnum'=>$stoke['carnum'],'cityid'=>$cityid))->select();
            foreach ($stocklist as $key => $value) {
                if(in_array($stoke['color'], $value) ){
                   $res = $stock->where('id='.$value['id'])->setInc('stock',$stoke['sockid']);
                   if($res){
                        $this->ajaxReturn(array('success'=>"OK",'msg'=>"添加成功"));
                   }
                }
            }
            $stock->add($sockarr);
            $this->ajaxReturn(array('success'=>"OK",'msg'=>"添加成功"));
        }else{
            $this->ajaxReturn(array('success'=>"ERROR",'msg'=>"添加失败"));
            
        }
    }
    public function car_price(){
        $getadd   = I("get.");
        $carprice = M('car_price');
        $list = $carprice->where(array('carnum'=>$getadd['carnum'],'is_new'=>1))->select();
        $this->assign('pricelist',$list);
        $this->display();
    }
    /**
     * 添加库存
     */
    public function addCarPrice(){
        $data   = I('post.');
        $Stock  = D('Stock');
        $res = $Stock->addModelStock($data);
        if($res['ret'] == "ERROR"){
            $this->ajaxReturn($res);
        }elseif($res){
            $this->ajaxReturn(array('ret'=>'OK','msg'=>'修改成功'));
        }else{
            $this->ajaxReturn(array('ret'=>'ERROR','msg'=>'修改失败'));
        }

    }

    /**
     * 删除库存
     */
    public function deletecangku(){
        $data = I('post.');
        $kuncunMode = M('cars_stock');
        $res = $kuncunMode->where(array('carnum'=>$data['carnum'],'cityid'=>$data['id']))->delete();
        if($res){
            $this->ajaxReturn(array('success'=>"OK",'msg'=>"删除成功"));
        }else{
            $this->ajaxReturn(array('success'=>"ERROR",'msg'=>"删除失败"));

        }
    }
    
    /**
     * 修改颜色
     */
    public function eidtstock(){
        $data = I("post.");
        $mode = M('cars_stock');
        $res  = $mode->where(array('id'=>$data['stockid']))->save(array('color'=>$data['color']));
        if($res){
            $this->ajaxReturn(['success'=>'OK','msg'=>'修改成功']);
        }else{
            $this->ajaxReturn(['success'=>'ERROR','msg'=>'修改失败']);

        }
    }
    
}